const cheerio = require('cheerio');
const _ = require('underscore');
const Base = require('./_base')
const path = window.require('path');
const md5 = require('js-md5');

export default class Gather extends Base{
	constructor(reportCb) {
	    super(reportCb);
	    this.enterurl = 'http://www.bellotti.it/'
	    this.host="http://www.bellotti.it/";
      this.pathname = path.resolve('data/' + md5(this.enterurl))
	  }
	  getProductUrls = async() => {
	  	let res = await fetch(this.enterurl).then(res => res.text());
	  	let $ = cheerio.load(res)
  		let goods_paths=[];
  	    let html = $('#nav-menu-item-490').html();
  		$('ul li',html).each(function(i,e){
  			product.cat=$('a',$(this).html()).text();
  			if($('a',$(this).html()).attr('href').split('bellotti').length>1){
  				goods_paths.push($('a',$(this).html()).attr('href'));
  			}
  		});
  		for (let i of _.range(goods_paths.slice(0,4).length)) {
  			 let url = goods_paths[i]
  			this.status.msg = '获取列表'
  			this.status.current = i + 1
  			this.status.count = goods_paths.length
  			this.reportCb(this.status)
  		}
  		return goods_paths;
	  }
	  getProduct = async(url) => {
	  	if(url.split('bellotti').length<1)return false;
	  	let res = await fetch(url).then(res => res.text());
	  	let $ = cheerio.load(res)
	  	var product={};
	  	let goods_list=$('.edgt_slider_inner').html();
	  	if($('ul li').length<1)return false;
	  	product.origin_html=$('html').html();
	  	product.origin_url = url;
	  	product.brand_num='bellotti';
	  	product.date=new Date().getTime();
	  	product.images=[];
	  	product.path=[];
	  	product.local_path=[];
	  	product.name=$('.section_inner_margin h1').text();
	  	product.description=$('.section_inner_margin p').text()
	  	$('ul li').each(function(j,el){
	  			if($('img',$(this).html()).attr('src')===undefined)
	  				return false;
	  			product.images.push({"pic_url":img});
	  	});
	  	return product;
	  }
}
